package com.ruoyi.project.filemgr.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.filemgr.domain.Filemgr;
import com.ruoyi.project.filemgr.service.IFilemgrService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 文件Controller
 * 
 * @author filemgr
 * @date 2024-02-02
 */
@RestController
@RequestMapping("/filemgr/filemgr")
public class FilemgrController extends BaseController {
    @Autowired
    private IFilemgrService filemgrService;

    /**
     * 查询文件列表
     */
    @PreAuthorize("@ss.hasPermi('filemgr:filemgr:list')")
    @GetMapping("/list")
    public AjaxResult list(Filemgr filemgr) {
        filemgr.setUserId(getUserId());
        List<Filemgr> list = filemgrService.selectFilemgrList(filemgr);
        for (Filemgr filemgr2 : list) {
            filemgr2.setFilemgr(filemgrService.selectFilemgrById(filemgr2.getpId()));
        }
        return success(list);
    }

    /**
     * 导出文件列表
     */
    @PreAuthorize("@ss.hasPermi('filemgr:filemgr:export')")
    @Log(title = "文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Filemgr filemgr) {
        List<Filemgr> list = filemgrService.selectFilemgrList(filemgr);
        ExcelUtil<Filemgr> util = new ExcelUtil<Filemgr>(Filemgr.class);
        util.exportExcel(response, list, "文件数据");
    }

    /**
     * 获取文件详细信息
     */
    @PreAuthorize("@ss.hasPermi('filemgr:filemgr:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(filemgrService.selectFilemgrById(id));
    }

    /**
     * 新增文件
     */
    @PreAuthorize("@ss.hasPermi('filemgr:filemgr:add')")
    @Log(title = "文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Filemgr filemgr) {
        filemgr.setUserId(getUserId());
        filemgr.setUserName(getLoginUser().getUser().getNickName());
        return toAjax(filemgrService.insertFilemgr(filemgr));
    }

    /**
     * 修改文件
     */
    @PreAuthorize("@ss.hasPermi('filemgr:filemgr:edit')")
    @Log(title = "文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Filemgr filemgr) {
        return toAjax(filemgrService.updateFilemgr(filemgr));
    }

    /**
     * 删除文件
     */
    @PreAuthorize("@ss.hasPermi('filemgr:filemgr:remove')")
    @Log(title = "文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(filemgrService.deleteFilemgrByIds(ids));
    }
}
